
package com.yurivolkov.android.locale_audio_update_notifier;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.yurivolkov.android.locale_audio_update_notifier.Constants;

/**
 * This is the "fire" {@code BroadcastReceiver} for a <i>Locale</i> plug-in
 * setting.
 */
public final class FireReceiver extends BroadcastReceiver {
    // log tag
    private static final String TAG = FireReceiver.class.getSimpleName();

    /**
     * @param context {@inheritDoc}.
     * @param intent the incoming {@code Intent}. This should always contain the
     *            store-and-forward {@code Bundle} that was saved by
     *            {@link EditActivity} and later broadcast by <i>Locale</i>.
     */
    @Override
    public void onReceive(final Context context, final Intent intent) {
        /*
         * Always be sure to be strict on your input parameters! A malicious
         * third-party app could always send your plug-in an empty or otherwise
         * malformed Intent. And since Locale applies settings in the
         * background, you don't want your plug-in to crash.
         */
        if (com.twofortyfouram.Intent.ACTION_FIRE_SETTING.equals(intent.getAction())) {
            Log.d(TAG, "onReceive");

            // 2011-07-09 As I was advised by Locale's Author
            // Parameters should be taken from the EXTRA_BUNDLE
            final Bundle bundle = intent.getBundleExtra(com.twofortyfouram.Intent.EXTRA_BUNDLE);
            
            // Delay in seconds
            final int delay = bundle.getInt(Constants.INTENT_EXTRA_DELAY, Constants.DELAY_DEFAULT);

            // Optional toast (for debugging purposes and for curious people)
            final String message = bundle.getString(Constants.INTENT_EXTRA_MESSAGE);

            Intent serviceIntent = new Intent(TheService.class.getName());
            serviceIntent.putExtra(Constants.INTENT_EXTRA_DELAY, delay);
            serviceIntent.putExtra(Constants.INTENT_EXTRA_MESSAGE, message);
            context.startService(serviceIntent);
        }
    }
}
